package adapter;

import java.util.List;
import android.bluetooth.BluetoothDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.ecdc.soexample.App;
import com.deep_tools.ecdc.R;

public class BluetoothDeviceAdapter extends BaseAdapter {
	private List<BluetoothDevice> listBluetoothDevice;

	public BluetoothDeviceAdapter(List<BluetoothDevice> listBluetoothDevice) {
		this.listBluetoothDevice = listBluetoothDevice;
	}
	public void addDevice(BluetoothDevice device) {
		if(!listBluetoothDevice.contains(device)) {
			listBluetoothDevice.add(device);
		}

    }
	public void updateBLDeviceList(){
		this.notifyDataSetChanged();
	}


	@Override
	public int getCount() {
		if (listBluetoothDevice == null || listBluetoothDevice.size() <= 0) {
			return 0;
		}
		return listBluetoothDevice.size();
	}

	@Override
	public Object getItem(int paramInt) {
		return null;
	}

	@Override
	public long getItemId(int paramInt) {
		return 0;
	}

	@Override
	public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {

		View view = null;
		final BluetoothDeviceAdapter.ViewHolder mHolder;
		if (paramView == null) {
			mHolder = new BluetoothDeviceAdapter.ViewHolder();
			view = LayoutInflater.from(App.context).inflate(R.layout.item_bluetooth_device_list, null);
			mHolder.bluetoothName=view.findViewById(R.id.bluetoothName);
			mHolder.bluetoothDeviceAddress=view.findViewById(R.id.bluetoothDeviceAddress);

			view.setTag(mHolder);

		}else{
			view = paramView;
			mHolder = (BluetoothDeviceAdapter.ViewHolder) view.getTag();
		}




//		View view=LayoutInflater.from(App.context).inflate(R.layout.item_bluetooth_device_list, null);
//		TextView bluetoothName=view.findViewById(R.id.bluetoothName);
//		TextView bluetoothDeviceAddress=view.findViewById(R.id.bluetoothDeviceAddress);
		String name=listBluetoothDevice.get(paramInt).getName();
		
		if(name==null||listBluetoothDevice.get(paramInt).getName().equals("")){
			mHolder.bluetoothName.setText("---");

		}else{
			mHolder.bluetoothName.setText(listBluetoothDevice.get(paramInt).getName());

		}
		mHolder.bluetoothDeviceAddress.setText(listBluetoothDevice.get(paramInt).getAddress());
		return view;
	}

	@Override
	public CharSequence[] getAutofillOptions() {
		return null;
	}


	class ViewHolder {
		TextView bluetoothName,bluetoothDeviceAddress;
	}

}
